package com.atguigu.ems.web.converters;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {

	private DateFormat dateFormat = null;
	
	{
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	}
	
	@Override
	public Object convertFromString(Map arg0, String[] args, Class arg2) {
		if(args != null && args.length == 1){
			try {
				return dateFormat.parse(args[0]);
			} catch (ParseException e) {}
		}
		
		return null;
	}

	@Override
	public String convertToString(Map arg0, Object val) {
		if(val instanceof Date){
			return dateFormat.format((Date)val);
		}
		return null;
	}

}
